#pragma once

#include <iostream>
#include <functional>
#include <string>
#include <cstdio>
#include <pthread.h>


using namespace std;

int num = 0;

class Thread
{
public:
    typedef function<void*(void*)> func;

    Thread(func f, void* args)
    : _f(f),
      _args(args)
    {
        char buffer[64];
        snprintf(buffer, sizeof buffer, "thread-%d", num++);
        _name = buffer;
        pthread_create(&_tid, nullptr, start_routine, this);
    }


    static void* start_routine(void* args)
    {
        Thread* t = (Thread*)args;
        return t->_f(t->_args);
    }

    string getThreadName()
    {
        return _name;
    }

    void join()
    {
        pthread_join(_tid, nullptr);
    }

private:
    pthread_t _tid;
    string _name;
    func _f;
    void* _args;
};
